1374B - Multiply by 2 divide by 6 - CodeForces Solution


math *900

Please click on ads to support us..

Python Code:

Test=int(input())
for J in range(Test):
    N=int(input())
    Cont=0
    Aux=0
    while(N!=1):
        if(N%6==0):
            N=N//6
            Cont+=1
        else:
            Aux=N*2
            if((Aux%6==0)or(Aux==6)):
                N=N*2
                Cont+=1
            else:
                Aux=-1
                break
    if(Aux==-1):
        print(-1)
    else:
        print(Cont)

C++ Code:

// Date: 2023-01-31 14:22:25
// Problem: B. Multiply by 2, divide by 6
// Contest: Codeforces - Codeforces Round #653 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1374/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// --------By WIDA--------

#include <bits/stdc++.h>
using namespace std;
namespace WIDA {
	using LL = long long;
	using PII = pair<LL, LL>;
	using TII = tuple<LL, LL, LL>;
	#define FOR(i,a,b) for (int i = (int)(a); i <= (int)(b); i ++)
	#define FOR2(i,a,b) for (int i = (int)(a); i <= (int)(b); i += 2)
	#define FORD(i,b,a) for (int i = (int)(a); i >= (int)(b); i --)
	#define ALL(a) a.begin(), a.end()
	#define rALL(a) a.rbegin(), a.rend()
	#define pb push_back
	#define fi first
	#define se second
	#define sz size()
	const LL INF = 0x3f3f3f3f3f3f3f3f;
	template <class... Args> void _(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    cout << "--->"; int arr[] = {(_(args), 0)...};
	    cout << "\n";
	}
	template <class T> void _i(T args) {
	    cout << "{";
	    for (auto i : args) cout << i << ", "; cout << "}\n";
	}
	template <class T> void _ii(T args) {
	    cout << "{"; 
	    for (auto [i, j] : args) cout << i << " " << j << ", ";
	    cout << "}\n";
	}
	template <class... Args> void __(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    int arr[] = {(_(args), 0)...}; cout << "\n";
	}
	#define int long long // Zmod要关闭
	#define endl "\n" // 交互题要关闭
	const int MOD = 998244353; // 看清楚每道题的 MOD
	const int N = 1e6 + 7; // 二维数组要修改 vector<vector<int> > a(n, vector<int> (m, 0));
}
using namespace WIDA;


bool Solve() {
	int n; cin >> n;
	int cnt = 0, ans = 0;
	while (n % 3 == 0) {
		if (n % 2) ++ ans, n /= 3;
		else n /= 6;
		++ cnt;
	}
	if (n == 1) cout << ans + cnt << endl;
	else cout << -1 << endl;
	return 0;
}
signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	int Task = 1;
	cin >> Task;
	for (int i = 1; i <= Task; ++ i) {
		Solve();
//		cout << (Solve() ? "YES" : "NO") << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence